#ifndef ID_IDENTIFICATION_H
#define ID_IDENTIFICATION_H
#include <Arduino.h>
#include "hall_id.h"
#include <Arduino.h>
#include <BLE2902.h>
#include <ArduinoJson.h>
#include "bsp_ble.h"
#include "hbot_motion.h"
#include "bsp_motor.h"
#include "ElectromagnetControl.h"
#include <string>
#include <vector>
#include "middle.h"
#include "A_Star.h"
#include "buzzer.h"

// 定义棋盘大小
extern char board[100];
extern char newBoard[100];
extern char previousBoard[100];
extern const int NUM_CONFIGS;
extern int moveCount;
// 串口定义
extern HardwareSerial mySerial;

// 变量定义
extern String rxBuffer;
extern String idBoard[10][10];
extern bool idMatrixReady;
extern String availableID;
extern bool gameStarted;
extern float hall_sensitivity_val;

// 互斥锁
extern SemaphoreHandle_t boardMutex;
// 定义一个结构体用于存储命令
struct Command
{
    String cmd_name;
    String play_as;
    String move;
    String piece;
    String startPos;
    String endPos;
    int number;
    String move_list;
    String path_move_list;
    String capture_square; // 存储被吃棋子的目标位置
    int level;
    int speed;
    int input_beep_volume;
    int isOpen;
    String id; // 新增: 存储id字段，用于get_id_board命令
};

// 创建一个队列句柄
extern QueueHandle_t commandQueue;

// 函数声明
String mapPiece(const String &piece);
String reverseMapPiece(const String &internalID);
bool isStandardPosition(int index);
int positionToIndex(const String &pos);
float getXFromPosition(String position, int magnetNum);
float getYFromPosition(String position, int magnetNum);
String indexToPosition(int index, bool useNumeric = false);
void printBoard(const char localBoard[100]);
void printIDBoard(const String localIDBoard[100]);
void receiveMatrix();
void sendIdBoardInChunks(String cmd_name, String status, String err);
void sendEmptyIdBoardInChunks();
void commandProcessorTask(void *pvParameters);
void resetBoard();
void magnet_pick(float x_start, float y_start, int magnet_num);
void magnet_move(float x_move, float y_move, int magnet_num_move);
void magnet_drop(float x_end, float y_end);
int getMagnetNumFromPosition(const String &startPos, const String &endPos);
int getMagnetNumFromPosition_DS(const String &startPos, const String &endPos);
String getCenter(const String &state);
bool centerHasObstacle(const String &center);
void moveMagnetAlongPath(const std::vector<std::pair<int, int>> &path_19x19, int magnetNum);

void scannerTask(void *pvParameters);
void send_print(char id[10][10]);
void take_back();
// 打印任务函数声明
void printerTask(void *pvParameters);
void auto_chess(String piece, String startPos, String endPos, int magnetNum);
void id_board_init();
void sendprint(String cmd_name, String status, String err, String id_board[10][10], String id);
void sendHumanMoveResponse(String status, String err, String human_move, String capture_square);
void send_hall_sensitivity(float sensitivity_value);

#endif
